<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        //**5. 程序实现对数据统计其出现的次数并按出现次数进行排序，
        //   [ 4 ,1,, 2, 1, 3, 2, 1, 4] 作为参数（参数可变）传入js方法中，控制台输出如下结果**

        let arr = [2, 4, 1, 2, 1, 3, , 1, 4]

        // 获取某个元素次数方法
        function count(num) {
            let counts = 0
            arr.forEach(item => {
                if (item === num) {
                    counts++
                }
            })
            return counts
        }

        // 次数
        let objArr = []
        let temArr = []
        arr.forEach(item => {
            if (!temArr.includes(item)) {
                temArr.push(item)
                objArr.push({
                    name: item,
                    count: count(item)
                })
            }
        })

        objArr.sort((obj1, obj2) => obj2.count - obj1.count)

        objArr.forEach(item => {
            console.log(`${item.name} 出现了 ${item.count} 次`)
        })
    </script>
</body>

</html>